package com.turing.data.scripts.contract.filter

import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils
import org.springframework.util.CollectionUtils
import com.turing.common.util.FileUtils
import com.turing.common.util.StringUtils

class ChangeOfContract_30203_10_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){

        def REGEX_CHANGE_2 = '合同的变更1.'

        List<TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size() && i <= 10; i++) {
                def drawResultVO = resultVOs.get(i)
                def childTaskSn = drawResultVO.childTaskSn
                if(null == childTaskSn || childTaskSn == ""){
//                    return  resultVOs
                    continue
                }
                def start = drawResultVO.start
                def end = drawResultVO.end
                def text = drawResultVO.text
                text = text.replaceAll("\n","")
                def textPathMap = dependParams.textPathMap
                def rawPath = textPathMap.get(drawResultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def index = 0
                if(text.contains(REGEX_CHANGE_2)){
                    index = text.indexOf(REGEX_CHANGE_2)
                    def expand = 0
                    def startOffset = index + 5
                    while( expand < 250 && rawText.length() > end + expand  ){
                        def expandStr = rawText.substring(start,end+expand)
                        def REGEX_STOP = /.*2\..*变更.*。.*/
                        if( expandStr =~ REGEX_STOP ){
                            def JSONTextMap = dependParams.JSONTextMap
                            def fileTextStr = ""
                            fileTextStr = JSONTextMap.get(childTaskSn)
                            TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(drawResultVO)
                            objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, expand , fileTextStr, rawText)
                            objectNew.text = rawText.substring( start + startOffset , end + expand  )
                            objectNew.businessValue = rawText.substring( start +startOffset, end + expand  )
                            trueValueSet.add(objectNew)
                            println "合同变更方式》》》》》》》》"
                            return trueValueSet
                        }
                        expand ++
                    }
                }
            }
        }

        def Regex_1 = /.*合同变更1*/
        def Regex_2 = /.*合同的变更1*/
        def Regex_3 = /.*合同的生效*/

        def textPathMap = dependParams.textPathMap

        if (resultVOs != null && resultVOs.size() > 0) {

            for (int i = 0; i < resultVOs.size(); i++) {
                if (StringUtils.isEmpty(resultVOs.get(i).childTaskSn)) {
                    continue
                }
                def rawPath = textPathMap.get(resultVOs.get(i).childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                def start = resultVOs.get(i).start
                def end = resultVOs.get(i).end
                while (expandOffset < 200 && start > expandOffset) {
                    def expandStr = rawText.substring(start - expandOffset, start)
                    if ( expandStr =~ Regex_1 || expandStr =~ Regex_2 ) {
                        def index = 0
                        if( expandStr.indexOf("变更") > -1){
                            index = expandStr.indexOf("变更") + "变更".length()
                        }
                        def JSONTextMap = dependParams.JSONTextMap
                        def fileTextStr = ""
                        fileTextStr = JSONTextMap.get(resultVOs.get(i).childTaskSn)
                        TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(resultVOs.get(i))
                        objectNew = TextUtils.correctPositionInfoByOffset(objectNew, index -expandOffset, 0 , fileTextStr, rawText)
                        objectNew.text = rawText.substring( start + index - expandOffset , end )
                        objectNew.businessValue = rawText.substring( start + index- expandOffset, end )
                        trueValueSet.add(objectNew)
                        return trueValueSet
                    }
                    expandOffset++
                }

            }
        }

        if (resultVOs != null && resultVOs.size() > 0) {

            for (int i = 0; i < resultVOs.size(); i++) {
                if (StringUtils.isEmpty(resultVOs.get(i).childTaskSn)) {
                    continue
                }
                def rawPath = textPathMap.get(resultVOs.get(i).childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                def start = resultVOs.get(i).start
                while (expandOffset < 500 && start > expandOffset) {
                    def expandStr = rawText.substring(start - expandOffset, start)

                    if ( expandStr =~ Regex_3 ) {
                        trueValueSet.add(resultVOs.get(i))
                        return trueValueSet
                    }
                    expandOffset++
                }

            }
        }

        if(!CollectionUtils.isEmpty(resultVOs) && resultVOs.size()>2){
            trueValueSet.add(resultVOs[0])
            trueValueSet.add(resultVOs[1])
            println trueValueSet
            return trueValueSet
        }

        return  resultVOs

    }

}
